const controller = require('../index')
const intervalCopyFileService = require('../../service/FileSystem/intervalCopyFile');
const {task} = require("../../service/FileSystem/intervalCopyFile");

exports.setIntervalCopyFile = (req, res) => {
  const { sourcePath, targetPath, duration = 1000 * 60 * 60 } = req.query
  if (!sourcePath || !targetPath) {
    return res.send(controller.errorParams({ msg: 'sourcePath和targetPath不能为空' }))
  }
  if (intervalCopyFileService.task.some(task => task.sourcePath === sourcePath && task.targetPath === targetPath)) {
    return res.send(controller.errorParams({ msg: `copy任务已存在，不能重复添加！：${task.length}` }))
  }

  const taskService = intervalCopyFileService.setIntervalCopyFile(sourcePath, targetPath, duration)

  if (taskService) {
    return res.send(controller.successParams({ msg: `任务添加成功！当前任务数：${task.length}` }))
  } else {
    return res.send(controller.errorParams({ msg: `任务添加失败！当前任务数：${task.length}` }))
  }

}

exports.clearIntervalCopyFile = (req, res) => {
  const { sourcePath, targetPath } = req.query
  const service = intervalCopyFileService.clearIntervalCopyFile(sourcePath, targetPath)

  if (service) {
    res.send(controller.successParams({ msg: `清除成功！当前任务数：${task.length}` }))
  } else {
    res.send(controller.errorParams({ msg: `找不到任务source和target！当前任务数：${task.length}` }))
  }
}
